package com.lzy.concertticket.util;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

@Component
public class ResourceUtil implements InitializingBean {

    private  static ClassLoader classLoader = ResourceUtil.class.getClassLoader();
    private  static String resourceRootPath;

    static {
        resourceRootPath = classLoader.getResource("").getPath();
        if (OperateSystemUtil.isWindows()) {
            resourceRootPath = resourceRootPath.substring(1);
        }
    }
    public static String getStaticRootPath() {
        return resourceRootPath + "static/";
    }
    public static String getVideoRootPath() {
        return resourceRootPath + "static/videos/";
    }
    public static String getImageRootPath() {
        return resourceRootPath + "static/images/";
    }
    public static String getAudioRootPath() {
        return resourceRootPath + "static/audios/";
    }
    public static String getFileRootPath() {
        return resourceRootPath + "static/files/";
    }



    public static String getMusicRootPath() {
        return resourceRootPath + "static/musics/";
    }

    public static String getStaticSourceCodeRootPath() {
        return resourceRootPath.replace("target/classes/", "src/main/resources/") + "static/";
    }
    public static String getVideoSourceCodeRootPath() {
        return resourceRootPath.replace("target/classes/", "src/main/resources/") + "static/videos/";
    }
    public static String getImageSourceCodeRootPath() {
        return resourceRootPath.replace("target/classes/", "src/main/resources/") + "static/images/";
    }
    public static String getAudioSourceCodeRootPath() {
        return resourceRootPath.replace("target/classes/", "src/main/resources/") + "static/audios/";
    }
    public static String getFileSourceCodeRootPath() {
        return resourceRootPath.replace("target/classes/", "src/main/resources/") + "static/files/";
    }


    public static String getMusicSourceCodeRootPath() {
        return resourceRootPath.replace("target/classes/", "src/main/resources/") + "static/musics/";
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //  判断运行时文件是否存在
        //  视频
        String videoRootPath = getVideoRootPath();
        File videoRootFile = new File(videoRootPath);
        if (!videoRootFile.exists()) {
            videoRootFile.mkdirs();
        }
        //  图片
        String imageRootPath = getImageRootPath();
        File imageRootFile = new File(imageRootPath);
        if (!imageRootFile.exists()) {
            imageRootFile.mkdirs();
        }
        //  音频
        String audioRootPath = getAudioRootPath();
        File audioRootFile = new File(audioRootPath);
        if (!audioRootFile.exists()) {
            audioRootFile.mkdirs();
        }
        //  文件
        String fileRootPath = getFileRootPath();
        File fileRootFile = new File(fileRootPath);
        if (!fileRootFile.exists()) {
            fileRootFile.mkdirs();
        }
        /*音乐*/
        //  视频
        String musicRootPath = getMusicRootPath();
        File musicRootFile = new File(musicRootPath);
        if (!musicRootFile.exists()) {
            musicRootFile.mkdirs();
        }


        //  判断源代码中的文件是否存在
        //  视频
        String videoSourceCodeRootPath = getVideoSourceCodeRootPath();
        File videoSourceCodeRootFile = new File(videoSourceCodeRootPath);
        if (!videoSourceCodeRootFile.exists()) {
            videoSourceCodeRootFile.mkdirs();
        }
        // 图片
        String imageSourceCodeRootPath = getImageSourceCodeRootPath();
        File imageSourceCodeRootFile = new File(imageSourceCodeRootPath);
        if (!imageSourceCodeRootFile.exists()) {
            imageSourceCodeRootFile.mkdirs();
        }
        //  音频
        String audioSourceCodeRootPath = getAudioSourceCodeRootPath();
        File audioSourceCodeRootFile = new File(audioSourceCodeRootPath);
        if (!audioSourceCodeRootFile.exists()) {
            audioSourceCodeRootFile.mkdirs();
        }
        //  文件
        String fileSourceCodeRootPath = getFileSourceCodeRootPath();
        File fileSourceCodeRootFile = new File(fileSourceCodeRootPath);
        if (!fileSourceCodeRootFile.exists()) {
            fileSourceCodeRootFile.mkdirs();
        }
        /*音乐*/
        //  视频
        String musicSourceCodeRootPath = getMusicSourceCodeRootPath();
        File musicSourceCodeRootFile = new File(musicSourceCodeRootPath);
        if (!musicSourceCodeRootFile.exists()) {
            musicSourceCodeRootFile.mkdirs();
        }
    }

    @PostConstruct
    public void init() {
        //  删除运行时目录
        //  图片
        File imageRootFile = new File(getImageRootPath());
        imageRootFile.listFiles(file -> {
            file.delete();
            return true;
        });
        //  视频
        File videoRootFile = new File(getVideoRootPath());
        videoRootFile.listFiles(file -> {
            file.delete();
            return true;
        });
        //  音频
        File audioRootFile = new File(getAudioRootPath());
        audioRootFile.listFiles(file -> {
            file.delete();
            return true;
        });
        //  文件
        File fileRootFile = new File(getFileRootPath());
        fileRootFile.listFiles(file -> {
            file.delete();
            return true;
        });


        //  将源代码目录下的资源同步到运行时目录下
        //  图片
        File imageSourceCodeRootFile = new File(getImageSourceCodeRootPath());
        imageSourceCodeRootFile.listFiles(file -> {
            if (file.isFile()) {
                //  获取目标文件名
                String targetFileName = getImageRootPath() + file.getName();
                System.out.println(targetFileName);
                //  复制文件
                try {
                    Files.copy(file.toPath(), new File(targetFileName).toPath());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        });
        //  视频
        File videoSourceCodeRootFile = new File(getVideoSourceCodeRootPath());
        videoSourceCodeRootFile.listFiles(file -> {
            if (file.isFile()) {
                //  获取目标文件名
                String targetFileName = getVideoRootPath() + file.getName();
                System.out.println(targetFileName);
                //  复制文件
                try {
                    Files.copy(file.toPath(), new File(targetFileName).toPath());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        });
        //  音频
        File audioSourceCodeRootFile = new File(getAudioSourceCodeRootPath());
        audioSourceCodeRootFile.listFiles(file -> {
            if (file.isFile()) {
                String targetFileName = getAudioRootPath() + file.getName();
                System.out.println(targetFileName);
                try {
                    Files.copy(file.toPath(), new File(targetFileName).toPath());
                }catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        });
        //  文件
        File fileSourceCodeRootFile = new File(getFileSourceCodeRootPath());
        fileSourceCodeRootFile.listFiles(file -> {
            if (file.isFile()) {
                String targetFileName = getFileRootPath() + file.getName();
                System.out.println(targetFileName);
                try {
                    Files.copy(file.toPath(), new File(targetFileName).toPath());
                }catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        });
    }
}
